/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.monitor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.monitor.dto.ApplicationDTO;
import org.springblade.modules.monitor.dto.ChainSourceDTO;
import org.springblade.modules.monitor.dto.ChartDTO;
import org.springblade.modules.monitor.dto.DatabaseInfoDTO;
import org.springblade.modules.monitor.entity.Application;
import org.springblade.modules.monitor.entity.DatabaseInfo;
import org.springblade.modules.monitor.pojo.*;
import org.springblade.modules.monitor.service.IHostInfoService;
import org.springblade.modules.monitor.vo.ChainSourceVO;
import org.springblade.modules.monitor.vo.HostInfoVO;
import org.springblade.modules.monitor.vo.ItemChartResult;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 *  控制器
 *
 * @author Blade
 * @since 2020-05-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-monitor/databaseinfo")
@Api(value = "数据库信息", tags = "数据库信息")
public class DatabaseInfoController extends BladeController {
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "数据库详情", notes = "数据库详情")
	public R<DatabaseDetail> detail(Long databaseId) {
		DatabaseInfo databaseInfo = new DatabaseInfo("192.168.11.22", "运维平台数据库", 1, 1521, "Oracle", "12C");
		DatabaseDetail detail = new DatabaseDetail();
		BeanUtils.copyProperties(databaseInfo, detail);

		List<TableWithIndex> tableWithIndices = new ArrayList<>();


		TableWithIndex tableWithIndex1 = new TableWithIndex();
		tableWithIndex1.setTableName("application_prototype");
		tableWithIndex1.setSize("1010M");
		List<TableIndex> tableIndices1 = new ArrayList<>();
		tableIndices1.add(new TableIndex("name",1));
		tableIndices1.add(new TableIndex("proxy",0));
		tableWithIndex1.setIndexList(tableIndices1);
		tableWithIndices.add(tableWithIndex1);

		TableWithIndex tableWithIndex2 = new TableWithIndex();
		tableWithIndex2.setTableName("config_autoreg_tls");
		tableWithIndex2.setSize("910M");
		List<TableIndex> tableIndices2 = new ArrayList<>();
		tableIndices2.add(new TableIndex("address",1));
		tableIndices2.add(new TableIndex("reg",1));
		tableWithIndex2.setIndexList(tableIndices2);
		tableWithIndices.add(tableWithIndex2);

		TableWithIndex tableWithIndex3 = new TableWithIndex();
		tableWithIndex3.setTableName("event_recovery");
		tableWithIndex3.setSize("885M");
		List<TableIndex> tableIndices3 = new ArrayList<>();
		tableIndices3.add(new TableIndex("name",1));
		tableIndices3.add(new TableIndex("proxy",0));
		tableWithIndex3.setIndexList(tableIndices3);
		tableWithIndices.add(tableWithIndex3);

		TableWithIndex tableWithIndex4 = new TableWithIndex();
		tableWithIndex4.setTableName("history_log");
		tableWithIndex4.setSize("882");
		tableWithIndices.add(tableWithIndex4);


		TableWithIndex tableWithIndex5 = new TableWithIndex();
		tableWithIndex5.setTableName("httpstep");
		tableWithIndex5.setSize("756M");
		List<TableIndex> tableIndices5 = new ArrayList<>();
		tableIndices5.add(new TableIndex("name",1));
		tableIndices5.add(new TableIndex("proxy",0));
		tableWithIndex5.setIndexList(tableIndices5);
		tableWithIndices.add(tableWithIndex5);

		TableWithIndex tableWithIndex6 = new TableWithIndex();
		tableWithIndex6.setTableName("item_application_prototype");
		tableWithIndex6.setSize("568M");
		List<TableIndex> tableIndices6 = new ArrayList<>();
		tableIndices6.add(new TableIndex("name",1));
		tableWithIndex6.setIndexList(tableIndices6);
		tableWithIndices.add(tableWithIndex6);

		TableWithIndex tableWithIndex7 = new TableWithIndex();
		tableWithIndex7.setTableName("mappings");
		tableWithIndex7.setSize("523M");
		List<TableIndex> tableIndices7 = new ArrayList<>();
		tableIndices7.add(new TableIndex("name",1));
		tableWithIndex7.setIndexList(tableIndices7);
		tableWithIndices.add(tableWithIndex7);

		TableWithIndex tableWithIndex8 = new TableWithIndex();
		tableWithIndex8.setTableName("opcommand_hst");
		tableWithIndex8.setSize("326M");
		List<TableIndex> tableIndices8 = new ArrayList<>();
		tableIndices8.add(new TableIndex("name",1));
		tableWithIndex8.setIndexList(tableIndices8);
		tableWithIndices.add(tableWithIndex8);

		TableWithIndex tableWithIndex9 = new TableWithIndex();
		tableWithIndex9.setTableName("acknowledges");
		tableWithIndex9.setSize("322M");
		List<TableIndex> tableIndices9 = new ArrayList<>();
		tableIndices9.add(new TableIndex("name",1));
		tableWithIndex9.setIndexList(tableIndices9);
		tableWithIndices.add(tableWithIndex9);

		TableWithIndex tableWithIndex10 = new TableWithIndex();
		tableWithIndex10.setTableName("dservices");
		tableWithIndex10.setSize("311M");
		List<TableIndex> tableIndices10 = new ArrayList<>();
		tableWithIndex10.setIndexList(tableIndices10);
		tableWithIndices.add(tableWithIndex10);

		detail.setTableWithIndices(tableWithIndices);

		List<TableSpace> tableSpaces = new ArrayList<>();
		TableSpace tableSpace1 = new TableSpace("sge_user","20G","11.6G","8.4G","58.00");
		tableSpaces.add(tableSpace1);
		TableSpace tableSpace2 = new TableSpace("business_user","20G","10.6G","9.4G","53.00");
		tableSpaces.add(tableSpace2);

		detail.setTableSpaces(tableSpaces);
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		detail.setCollectTime(LocalDateTime.now().format(formatter));
		detail.setCpuUsage(11.35);
		detail.setMemUsage(30.23);
		detail.setDeadLockNum(0);
		detail.setDictionaryCachePercent(99.99);
		detail.setSgaPercent(98.89);
		detail.setMaxSessions(200);
		detail.setNowConnectSessions(10);
		detail.setTableSpaceRatio(50.00);
		return R.data(detail);
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入ChainSourceVO")
	public R<IPage<DatabaseInfo>> page(DatabaseInfoDTO chainSourceDTO, Query query) {
		List list = new ArrayList();
		DatabaseInfo databaseInfo = new DatabaseInfo("192.168.11.22","运维平台数据库",1,1521,"Oracle","12C");
		list.add(databaseInfo);
		IPage<DatabaseInfo> pages = new Page<DatabaseInfo>();
		pages.setRecords(list);
		pages.setTotal(1);
		pages.setCurrent(query.getCurrent());
		pages.setSize(query.getSize());
		return R.data(pages);
	}

}
